<template>
  <avue-crud :class="type=='view'?'top15':''" :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form" @row-update="addUpdate" @row-del="rowDel" @row-save="rowSave" @refresh-change="refreshChange" @on-load="onLoad">
    <template slot="menuLeft">
      <el-button type="text" icon="el-icon-plus" class="rightbtn" @click="addRow">新增</el-button>
    </template>
  </avue-crud>
</template>
<script>
import { getProOutProcessList } from '@/api/project/productData/processData';
import { numparZReg, numParseReg, codeReg } from '@/util/zhengze';
import { init } from '@/mixins/init';
export default {
  mixins: [init],
  props: {
    projectId: {
      type: String,
    },
    productId: {
      type: String,
    },
    materialName: {
      type: String,
    },
    type: {
      type: String,
    },
    sopform: {
      type: Object,
    },
    data: {
      type: Array,
    },
  },
  data() {
    return {
      form: {},
      procedureOptions: [],
      customerDrawingNo: '',
      customerName: '',
      produceShopName: '',
      equipmentList: [],
      option: {
        index: true,
        indexLabel: '序号',
        // menu: false,
        menuWidth: 130,
        addBtn: false,
        editBtn: false,
        // delBtn: false,
        addRowBtn: false,
        cellBtn: true,
        column: [
          {
            label: '外协工序',
            prop: 'procedureName',
            type: 'select',
            dicData: [],
            minWidth: 130,
            cell: true,
            rules: [
              {
                required: true,
                message: '请选择外协工序',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '外协内容说明',
            prop: 'content',
            maxlength: 15,
            span: 12,
            minWidth: 150,
            cell: true,
            overHidden: true,
          },
          {
            label: '加工件数',
            prop: 'count',
            minWidth: 100,
            cell: true,
            type: 'number',
            minRows: 1,
            maxRows: 99999,
            overHidden: true,
            // rules: [
            //   {
            //     required: true,
            //     type: 'number',
            //     message: '请输入加工件数',
            //     trigger: 'blur',
            //   },
            // ],
          },
          {
            label: '技术要求',
            prop: 'requirement',
            span: 12,
            minWidth: 150,
            cell: true,
            overHidden: true,
            maxlength: 20,
          },
          {
            label: '推荐供应商',
            minWidth: 150,
            overHidden: true,
            span: 12,
            maxlength: 30,
            prop: 'supplier',
            cell: true,
          },
          {
            label: '验收日期',
            prop: 'acceptanceDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 200,
            cell: true,
          },
        ],
      },
    };
  },
  created() {
    if (this.type == 'edit') {
      this.option.header = true;
      this.option.menu = true;
    } else {
      this.option.header = false;
      this.option.menu = false;
    }
    getProOutProcessList({ productId: this.productId }).then((res) => {
      const data = res.data.data;
      this.findObject(this.option.column, 'procedureName').dicData = data.map((item) => {
        return {
          label: item,
          value: item,
        };
      });
    });
  },
  watch: {},
  methods: {
    addRow() {
      this.$refs.crud.rowCellAdd({
        count: 1,
      });
    },
    rowSave(row, done, loading) {
      if (!numParseReg.test(row.count)) {
        this.$message.error('加工件数请输入大于0的整数');
        loading();
        return;
      }
      this.data.push(row);
      done();
    },
    addUpdate(row, index, done, loading) {
      if (!numParseReg.test(row.count)) {
        this.$message.error('加工件数请输入大于0的整数');
        loading();
        return;
      }
      this.data.splice(index, 1, row);
      done();
    },
    rowDel(row, index) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.data.splice(index, 1);
      });
    },
  },
};
</script>
<style lang="scss">
.top15 {
  margin-top: 15px;
}
</style>